<?php 
// test.....
ActiveMongo::connect(sfConfig::get('app_mongo_db'), sfConfig::get('app_mongo_host'), sfConfig::get('app_mongo_username'), sfConfig::get('app_mongo_password'));

class publisher extends ActiveMongo {

    public $username;
    public $pwd;
    public $address;
    public $country;
    public $date;

    static function newInstance() {
        if (isset($this)) {
            return get_class($this);
        } else {
            return get_called_class();
        }
    }

    function username_filter($value, $old_value) {
        if ($old_value != null && $value != $old_value) {
            throw new ActiveMongo_FilterException("The username cant be changed");
        }
        if (!preg_match("/^[A-Za-z]+$/", $value)) {
            throw new ActiveMongo_FilterException("this username isnt valid");
        }
        if (strlen($value) < 5) {
            throw new ActiveMongo_FilterException("name 2 short!");
        }
        return TRUE;
    }

    function pwd_filter($value1) {
        if (strlen($value1) < 5) {
            throw new ActiveMongo_FilterException("PWD cant less than 5");
            return FALSE;
        }
        $value1 = sha1($value1);
    }

    function getCollectionName() {
        return "author";
    }

    function before_validate($obj) {
        $obj['date'] = new MongoTimeStamp();
    }
        function after_create()
    {
        $this->flag = 1;
        $this->save();
    }
    
        function pre_save($op, $document)
    {
        $to_check = array('username', 'password');

        switch($op) {
        case 'create':
            foreach ($to_check as $field) {
               if (!isset($document[$field])) {
                   throw new FilterException("Missing field {$field}");
               }
            }
            break;
        case 'update':
            foreach ((array)$document['$unset'] as $field) {
                if (isset($document['$unset'][$field])) {
                    throw new FilterException("The field {$field} cant be unset");
                }
            }
            break;
        }
    }

}
echo publisher::newInstance();
$user = publisher::newInstance();
$user = new publisher();
$user->username = "lajaa";
$user->pwd = "111121";
$user->country = "UKkkjk";
$user->address = "rue du";
$user->save();
/* would throw the error "cant be changed"
  $user->username = "sdfdf";
  $user->save(); */
echo $user->getCollectionName();

var_dump($user->getArray());
echo $user->getID();
var_dump($user->getIndexes());

$user->reset();

$user->find(array('country' => 'UKkk'));
echo $user->count();
foreach ($user as $key => $value) {
    var_dump($value['username']); //access FIND result
    // $value['username']="haha"; // update every properties requested
    // $user->save();
    //var_dump($user); // datas exist stop here
}
//var_dump($user); // would be null
?> 


